from django.contrib import admin
from django import forms
from django.shortcuts import get_object_or_404

from apps.Basic_data.models import Product
from apps.suppliers.models import PurchaseOrder, PurchaseReceiptDetail, PurchaseReturnDetail,\
    PurchaseReceipt, PurchaseOrderDetail, PurchaseReturn
from django.forms import inlineformset_factory

from django.contrib.admin.utils import display_for_field

# 注册模型


# 设置Django admin的header、title和index_title
admin.site.site_header = 'GanEr'
admin.site.site_title = 'GanEr'
admin.site.index_title = 'GanEr'


class PurchaseOrderDetailForm(forms.ModelForm):
    # product_name = forms.CharField()

    class Meta:
        model = PurchaseOrderDetail
        fields = ['product', 'purchase_order_code', 'quantity']
        list_display = ['product', 'purchase_order_code', 'quantity']

    # def __init__(self, *args, **kwargs):
    #     super().__init__(*args, **kwargs)
    #     # BUG : 这里需要判断self.instance是否存在，否则会报错
    #     # 必须用try
    #     try:
    #         if self.instance and self.instance.product:
    #             self.fields['product_name'].initial = self.instance.product.name
    #     except: pass
    #
    #     self.fields['product_name'].widget.attrs.update({'readonly': True})
    #     self.fields['product_name'].label = '产品名称'
    #     self.fields['product_name'].help_text = ''
    #     self.fields['product_name'].disabled = True
    #     self.fields['purchase_order_code'].label = '采购订单号'


# PurchaseOrderDetailTabularInline类定义了一个用于在PurchaseOrderAdmin中显示PurchaseOrderDetail的TabularInline
class PurchaseOrderDetailTabularInline(admin.TabularInline):
    extra = 0
    form = PurchaseOrderDetailForm
    model = PurchaseOrderDetail
    raw_id_fields = ('product',)
    autocomplete_fields = ['product']


# PurchaseReceiptDetailTabularInline类定义了一个用于在PurchaseReceiptAdmin中显示PurchaseReceiptDetail的TabularInline
class PurchaseReceiptDetailTabularInline(admin.TabularInline):
    model = PurchaseReceiptDetail
    original = False
    extra = 0
    raw_id_fields = ('purchase_order_detail',)
    fields = ('purchase_order_detail', 'purchase_receipt_code', 'quantity_received', )
    autocomplete_fields = ['purchase_order_detail']
    # autocomplete_fields = ['purchase_order_detail']


# PurchaseReturnDetailTabularInline类定义了一个用于在PurchaseReturnAdmin中显示PurchaseReturnDetail的TabularInline
class PurchaseReturnDetailTabularInline(admin.TabularInline):
    model = PurchaseReturnDetail
    # show_change_link = False
    original = False
    extra = 0
    raw_id_fields = ('purchase_order_detail',)
    fields = ('purchase_order_detail', 'purchase_return_code', 'quantity_returned', )
    autocomplete_fields = ['purchase_order_detail']


# PurchaseOrderForm类定义了一个用于创建PurchaseOrder的ModelForm
class PurchaseOrderForm(forms.ModelForm):

    class Meta:
        model = PurchaseOrder
        fields = ('purchase_order_code', 'vendor', 'order_date', 'order_status')
        widgets = {

        }


# PurchaseOrderAdmin类定义了一个用于管理PurchaseOrder的ModelAdmin
@admin.register(PurchaseOrder)
class PurchaseOrderAdmin(admin.ModelAdmin):
    form = PurchaseOrderForm
    inlines = [PurchaseOrderDetailTabularInline]
    date_hierarchy = 'order_date'
    list_display = ('purchase_order_code', 'vendor', 'order_date', 'order_status')
    list_filter = ('vendor', 'purchase_order_code', 'order_date')
    search_fields = ('vendor', 'purchase_order_code')


# PurchaseReceiptAdmin类定义了一个用于管理PurchaseReceipt的ModelAdmin
@admin.register(PurchaseReceipt)
class PurchaseReceiptAdmin(admin.ModelAdmin):
    inlines = [PurchaseReceiptDetailTabularInline]
    date_hierarchy = 'order_date'
    fields = ['purchase_receipt_code', 'vendor', 'order_date', 'order_status']
    list_display = ('purchase_receipt_code', 'vendor', 'order_date', 'order_status')
    list_filter = ('vendor', 'purchase_receipt_code', 'order_date')
    search_fields = ('vendor', 'purchase_receipt_code')


@admin.register(PurchaseReturn)
class PurchaseReturnAdmin(admin.ModelAdmin):
    model = PurchaseReturn
    inlines = [PurchaseReturnDetailTabularInline]
    # date_hierarchy = 'order_date'
    fields = ['purchase_return_code', 'vendor', 'status', ]
    list_display = ['purchase_return_code', 'vendor', 'status', ]
    list_filter = ['purchase_return_code', 'vendor', 'status']
    search_fields = ['purchase_return_code', 'vendor', 'status']
    # autocomplete_fields = ('product', )


@admin.register(PurchaseOrderDetail)
class PurchaseOrderDetailAdmin(admin.ModelAdmin):
    model = PurchaseOrderDetail
    # date_hierarchy = 'order_date'

    fields = ['purchase_order_code', 'product', 'quantity']
    list_display = ('purchase_order_code', 'product', 'quantity', 'get_received_quantities',
                    'get_returned_quantities', 'get_not_received_quantities')
    list_filter = ('product', 'purchase_order_code', )
    search_fields = ('vendor', 'purchase_receipt_code', 'product')
    autocomplete_fields = ('product', )


@admin.register(PurchaseReceiptDetail)
class PurchaseReceiptDetailAdmin(admin.ModelAdmin):
    model = PurchaseReceiptDetail
    # date_hierarchy = 'order_date'
    fields = ['purchase_receipt_code', 'quantity_received', 'purchase_order_detail']
    list_display = ('purchase_receipt_code', 'quantity_received', 'purchase_order_detail')
    list_filter = ('purchase_receipt_code', )
    search_fields = ('purchase_order_detail', 'purchase_receipt_code')
    # autocomplete_fields = ('product', )
    # raw_id_fields = ('product', )
    # readonly_fields = ('product', )
    # exclude = ('product', )
    # filter_horizontal = ('product', )


@admin.register(PurchaseReturnDetail)
class PurchaseReturnDetailAdmin(admin.ModelAdmin):
    model = PurchaseReturnDetail
    fields = ['purchase_return_code', 'purchase_order_detail', 'quantity_returned', 'reason']
    list_display = ['purchase_return_code', 'purchase_order_detail', 'quantity_returned', 'reason']
    list_filter = ['purchase_return_code', ]
    search_fields = ['purchase_return_code', ]

